'use strict';


import Base from './base.js';

const querystring = require('querystring');
var ttutil = require("../../business/ttutil");
var http = require('http');
const getToken = require('../../business/getwebhongbaotoken.js');
const getUserInfo = require('../../business/getwebuserinfo.js');
import DBHelper from '../../business/db_helper.js';
import { isNull } from 'util';
export default class extends Base {

    
    async indexAction() {
		
            var that=this;
        try
        {
        //微信登录回调
        getToken(this.param("code")).then(function (data) {
			//id=this.param("id");
				//console.log("ididididid=========="+id);
                //console.log(JSON.parse(data));
            return JSON.parse(data);
            
            }).then(function (data) {
            //console.log("==========="+data['access_token']);
			//console.log("access_token======"+data['access_token']);
            //console.log("openid======"+data['openid']);
            getUserInfo(data['access_token'], data['openid']).then(  function (userinfo){
                //{ openid: 'oFZTB1b1TC0ud1zTnX7KOtE6RNpY',
                //nickname: '...',
                //sex: 1,
                //language: 'zh_CN',
                //city: '广州',
                //province: '广东',
                //country: '中国',
                //headimgurl: 'http://thirdwx.qlogo.cn/mmopen/vi_32/zWXSkANEsFM3ywJ1dnIQwtSgXuicwnne21sicbSkxaamj76kEamGBIsvK3F3D1A1ZYFynOzeYHBupnZVJ3Utwm7Q/132',
                //privilege: [],
                //unioonid: 'omQhcwkp-EpLtSwLUv-4XPjOgQe0' }
                //var arr= JSON.stringify(userinfo);  
                //console.log(arr);
                userinfo= JSON.parse(userinfo);
                var openid=userinfo['openid'];
                var unionid=userinfo['unionid'];
                var nickname=userinfo['nickname'];

                //openid='abcdef1111111';

                //unionid="abcdef";

                // console.log("openid============="+openid);
                // console.log("unionid============="+unionid);
                // console.log("nickname============="+nickname);

                
                //console.log("-------------"+id);

            //向辅助服务发送此链接点击事件
			//console.log("id====="+that.param("id"));
			//console.log(unionid+"aaaaaaaaaa"+id);
            if(openid!=null&&unionid!=null){
                    //console.log(unionid+"aaaaaaaaaa"+id);
                    //console.log("=================");
                    DBHelper.updateUnionid(unionid,openid);
                
                    }
			
                });
            })

            }
            catch(err){return this.display("");}
  

            
        
            this.display("");
        
    }
}